package com.zw.mes.module.assemble.controller;

import com.zw.mes.base.BaseController;
import com.zw.mes.common.JsonResult;
import com.zw.mes.module.assemble.entity.AsseAssemblyRoute;
import com.zw.mes.module.assemble.model.ApplyModel;
import com.zw.mes.module.assemble.service.ApplyService;
import com.zw.mes.module.system.mapper.UserMapper;
import com.zw.mes.module.transpositionManager.model.Select2Model;
import com.zw.mes.util.ShiroUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Description: 装配派工单 申请质检、历史申请
 * <p>
 * 权限：
 * @Author: China.wsg
 * @Date: 2021/04/22 16:12
 */
@Controller
@RequestMapping("/assemble/apply")
@SuppressWarnings("all")
public class ApplyController extends BaseController {

    @Autowired
    private ApplyService applyService;
    @Autowired
    private UserMapper userMapper;

    @GetMapping("/addPage")
    @RequiresPermissions({"asseOrd:testings:view"})
    public String addPage(Model model) {
        model.addAttribute("user", userMapper.findByAccount(ShiroUtils.getUser().getAccount()));
        return "assemble/apply/applyAdd";
    }

    /**
     * 查询待装配申请质检列表
     *
     * @param barCode 派工单二维码 规则：生产令号|图号|部分ID|2
     * @return
     */
    @ResponseBody
    @PostMapping("/appList")
    @RequiresPermissions({"asseOrd:testings:view"})
    public JsonResult appList(String barCode) {
        startPage();
        List<ApplyModel> list = applyService.findAppListByBarCode(barCode);
        return jsonResult(list);
    }

    /**
     * 获取最新可用数量
     *
     * @param id
     * @return
     */
    @ResponseBody
    @PostMapping("/getNewMayApplyNum")
    @RequiresPermissions("asseOrd:testings:view")
    public JsonResult getNewMayApplyNum(Integer id) {
        return success().setData(applyService.getNewMayApplyNum(id));
    }

    /**
     * 申请质检
     *
     * @param id
     * @return
     */
    @ResponseBody
    @PostMapping("/apply")
    @RequiresPermissions({"asseOrd:testings:commit"})
    public JsonResult applay(AsseAssemblyRoute route) {
        return applyService.apply(route);
    }

    /**
     * 历史申请 页面
     *
     * @return
     */
    @GetMapping("/selPage")
    @RequiresPermissions({"asseOrd:history:view"})
    public String selPage() {
        return "assemble/apply/applySel";
    }

    /**
     * 派工单-历史申请列表
     *
     * @param applyModel
     * @return
     */
    @ResponseBody
    @PostMapping("/selList")
    @RequiresPermissions({"asseOrd:history:view"})
    public JsonResult selList(ApplyModel applyModel) {
        startPage();
        List<ApplyModel> list = applyService.findHistoryApplyListByQuery(applyModel);
        return jsonResult(list);
    }

    /**
     * 撤销质检申请
     *
     * @param id
     * @return
     */
    @ResponseBody
    @PostMapping("/cancel")
    @RequiresPermissions({"asseOrd:history:check"})
    public JsonResult cancel(Integer id) {
        return jsonResult(applyService.cancelApply(id));
    }

    /**
     * 重新申请
     *
     * @param id
     * @return
     */
    @ResponseBody
    @PostMapping("/repeatApply")
    @RequiresPermissions({"asseOrd:history:check"})
    public JsonResult repeatApply(Integer id) {
        return jsonResult(applyService.repeatApply(id));
    }

    /**
     * @Author wsg
     * @Date 10:52 2021/5/13
     * @Description 获取接收的生产令列表
     **/
    @ResponseBody
    @PostMapping("/getRecProdNoList")
    public JsonResult getRecProdNoList(String prodNo) {
        return success().setData(applyService.getRecProdNoList(prodNo));
    }

    /**
     * @Author wsg
     * @Date 10:52 2021/5/13
     * @Description 获取接收的类型列表
     **/
    @ResponseBody
    @PostMapping("/getRecProdNoSpecialList")
    public JsonResult getRecProdNoSpecialList(String prodNo) {
        return success().setData(applyService.getRecProdNoSpecialList(prodNo));
    }

    /**
     * @Author wsg
     * @Date 11:10 2021/5/13
     * @Description 获取接收的部分列表
     **/
    @ResponseBody
    @PostMapping("/getRecProdNoPartList")
    public JsonResult getRecProdNoPartList(String prodNo, String special) {
        return success().setData(applyService.getRecProdNoPartList(prodNo, special));
    }

    /**
     * @Author wsg
     * @Date 11:10 2021/5/13
     * @Description 获取接收的图号列表
     **/
    @ResponseBody
    @PostMapping("/getRecProdNoDrawNoList")
    public JsonResult getRecProdNoDrawNoList(String prodNo,String special,Integer partId) {
        return success().setData(applyService.getRecProdNoDrawNoList(prodNo,special,partId));
    }

}
